<template>
  <div class="box">
    <el-form
      :inline="true"
      :model="formInline"
      label-width="80px"
      class="demo-form-inline"
    >
      <el-form-item label="学号">
        <el-input v-model="formInline.id" placeholder="请输入学号"></el-input>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="formInline.name" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="年龄">
        <el-input v-model="formInline.age" placeholder="请输入年龄"></el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-select v-model="formInline.sex" placeholder="请选择性别">
          <el-option label="男" value="男"></el-option>
          <el-option label="女" value="女"></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="班级">
      <el-select v-model="formInline.classid" placeholder="请选择班级">
        <el-option label="区域一" value="shanghai"></el-option>
        <el-option label="区域二" value="beijing"></el-option>
      </el-select>
    </el-form-item> -->
      <el-form-item label=" ">
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button type="primary" @click="addStu">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="
        tableData.slice((currentPage - 1) * pageSize, currentPage * pageSize)
      "
      border
      style="width: 100%"
      height="100%"
    >
      <el-table-column align="center" prop="s_id" label="学号" width="180" />
      <el-table-column align="center" prop="s_name" label="姓名" width="180" />
      <el-table-column align="center" prop="s_age" label="年龄" width="180" />
      <el-table-column align="center" prop="s_sex" label="性别" width="180" />
      <el-table-column align="center" label="状态" width="180">
        <template slot-scope="scope">
          <div>
            {{ scope.row.s_status === 1 ? "在读" : "已退学" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="s_create_time" label="入学时间" />
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableData.length"
    >
    </el-pagination>
    <AddDialog
      :visible.sync="visible"
      :operateType="operateType"
      :rowData="operateType === 1 ? {} : rowData"
      @handclose="handclose"
    />
  </div>
</template>

<script>
import {
  // insertStuApi,
  deleteStuApi,
  // updateStuApi,
  queryStuApi
} from '../../api/studentapi'
export default {
  data () {
    return {
      visible: false,
      operateType: 1, // 1新增2编辑
      rowData: {},
      pageSize: 5,
      currentPage: 1,
      tableData: [{ s_name: 'xxx', s_id: 'xxx' }],
      formInline: {
        id: '',
        name: '',
        age: '',
        sex: ''
      }
    }
  },
  components: {
    AddDialog: () => import('./add_com.vue')
  },
  methods: {
    argTran (args) {
      Object.keys(args).forEach((item) => {
        if (args[item] === '' || args[item] === null) {
          delete args[item]
        }
      })
      return args
    },
    async onSubmit () {
      console.log('----------1')
      const { data } = await queryStuApi({ ...this.argTran(this.formInline) })
      if (data.code === 200) {
        this.tableData = data.data
      } else {
        this.$message.error(`${data.code}${data.msg ? data.msg : '系统繁忙'}`)
      }
      console.log('-----查询接口', data)
    },
    async deleteStu (args) {
      const class_id = args.c_id
      const student_id = args.s_id
      const { data } = await deleteStuApi({ class_id, student_id })
      if (data.code === 200) {
        this.$message({
          message: '删除成功',
          type: 'success'
        })
        this.onSubmit()
      }
    },
    addStu () {
      this.operateType = 1
      this.visible = true
    },
    handleEdit (args) {
      this.operateType = 2
      this.rowData = args
      this.visible = true
      console.log('-----------edit', args)
    },
    handleDelete (args) {
      console.log('--------del', args)
      this.$confirm('请确认是否继续删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.deleteStu(args)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    handclose (args) {
      this.visible = false
      const { classid, name, age, sex } = args
      this.formInline.id = classid
      this.formInline.name = name
      this.formInline.age = age
      this.formInline.sex = sex
      this.onSubmit()
    },
    handleSizeChange (args) {
      this.pageSize = args
    },
    handleCurrentChange (args) {
      this.currentPage = args
    }
  }
}
</script>

<style lang="less" scoped>
.box {
  height: 70%;
  ::v-deep .el-input__inner {
    width: 220px;
  }
  .el-pagination {
    text-align: right;
    padding-right: 30px;
    ::v-deep .el-input__inner {
      width: 80px;
    }
  }
}
</style>
